<?php
session_start();
require '../nette/Nette/loader.php';
require '../db.php';
require '../localization/lang.php';
require '../core/core.sanitizer.php';
require '../core/core.security.php';
require '../core/core.dfs.php';
require '../visual/visual.dfs.php';
require '../core/core.time.php';
require '../core/core.statistics.php';

function getChildrenLit($rowid) {
$out="";

//echo $id[];
$query="select left_state,right_state from tree where ID= ".$rowid."";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$left_state=$row['left_state'];
$right_state=$row['right_state'];

$query="select ID from tree where left_state > ".$left_state." AND right_state < ".$right_state."";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
	$out=$out."#row_".$row['ID'].",";
	}
return $out;
}


$login=new core_security;
if ($login->checklogin()=="OK" AND $_SESSION['role']<=1) {






$loc=new loc;
$datetime=new core_time;
$datefrom=explode("/",$_POST['datefrom']);
$datetime->date=$datefrom[1].":".$datefrom[0].":".$datefrom[2];
$datetime->time="00:01";
$from=$datetime->totimestamp();
$dateto=explode("/",$_POST['dateto']);
$datetime->date=$dateto[1].":".$dateto[0].":".$dateto[2];
$datetime->time="23:59";
$to=$datetime->totimestamp();
$statist=new core_statistics_advanced($_POST['datefrom'],$_POST['dateto'],0);
$counter=0;

function indent($level){
	$out="";
	for ($i = 0; $i < ($level-1); $i++) {
		$out=$out." > ";
		}
	return $out;
	}

$users="";

if ($_POST['datefrom'] != "" AND $_POST['dateto'] != "") {
if ($_POST['style'] == "both") {
echo "<b>".$loc->statisticsfor.$_POST['datefrom']." - ".$_POST['dateto']."</b><br>";
echo "<div class='maindiv'id='attendanceshow'><table id='statstable'>";


//first header row
$query="select name from users where statistics=1 ORDER BY id";
$result=mysql_query($query);
echo "<tr ><th> </th>";
while ($row=mysql_fetch_array($result)) {
echo "<th class='statsheader' colspan=2>".$row['name']."</th>";
}
echo "<th colspan=2 class='statsheader'> TOTAL TIME </th></tr>";


//second header row
$query="select name from users where statistics=1 ORDER BY id";
$result=mysql_query($query);
echo "<tr ><th> </th>";
while ($row=mysql_fetch_array($result)) {
echo "<th class='statsheader'>real</th><th class='statsheader'>acc</th>";
}
echo "<th class='statsheader'> real</th><th class='statsheader'> accumulated </th> </tr>";


$query2="select ID,name,LEVEL from tree WHERE statistics = 1 AND ID != 1 ORDER BY left_state ";
$result2=mysql_query($query2);
while ($row2=mysql_fetch_array($result2)) {
	$indent=indent($row2['LEVEL']);
	if ($statist->job_on_users($row2['ID']) !=0 ) {
	echo "<tr class='statsok' id='row_".$row2['ID']."' onmouseover='childrenlit2(\"".getChildrenLit($row2['ID'])."\");' onmouseout='childrenunlit2(\"".getChildrenLit($row2['ID'])."\");'><td  width='400px'><b>".$indent.$row2['name']."</b></td>";
	$query3="select id from users WHERE statistics = 1 ORDER BY id";
	$result3=mysql_query($query3);
	
	while ($row3=mysql_fetch_array($result3)) {
		echo "<td class='stats' onClick='showdetails(\"".$row2['ID']."\",\"".$row3['id']."\");'>".$datetime->tohm($statist->return_real_time($row2['ID'],$row3['id']))."</td><td class='stats' onClick='showdetails(\"".$row2['ID']."\",\"".$row3['id']."\");'>".$datetime->tohm($statist->job_on_user($row2['ID'],$row3['id']))."</td>";
		}
	echo "<td class='stats'><b>".$datetime->tohm($statist->return_real_time_allusers($row2['ID']))."</b></td><td class='stats'><b>".$datetime->tohm($statist->job_on_users($row2['ID']))."</b></td></tr>";
	}
	}

echo "<tr ><th></th>";
$query3="select id from users WHERE statistics = 1 ORDER BY id";
$result3=mysql_query($query3);
while ($row3=mysql_fetch_array($result3)) {
	echo "<th class='statsheader' colspan=2>".$datetime->tohm($statist->user_on_jobs($row3['id']))."</th>";
	}
echo "<th></th></tr></table></div>";


}





if ($_POST['style'] == "acc") {



echo "<b>".$loc->statisticsfor.$_POST['datefrom']." - ".$_POST['dateto']."</b><br>";
echo "<div class='maindiv'id='attendanceshow'><table id='statstable'>";



$query="select name from users where statistics=1 ORDER BY id";
$result=mysql_query($query);
echo "<tr ><th> </th>";
while ($row=mysql_fetch_array($result)) {
echo "<th class='statsheader'>".$row['name']."</th>";
}
echo "<th class='statsheader' colspan=2 >TOTAL</th></tr>";


$query="select name from users where statistics=1 ORDER BY id";
$result=mysql_query($query);
echo "<tr ><th> </th>";
while ($row=mysql_fetch_array($result)) {
echo "<th class='statsheader'>time</th>";
}
echo "<th class='statsheader'> real </th><th class='statsheader'> accumulated </th> </tr>";




$query2="select ID,name,LEVEL from tree WHERE statistics = 1 AND ID != 1 ORDER BY left_state ";
$result2=mysql_query($query2);
while ($row2=mysql_fetch_array($result2)) {
	$indent=indent($row2['LEVEL']);
	if ($statist->job_on_users($row2['ID']) !=0 ) {
	echo "<tr class='statsok' id='row_".$row2['ID']."' onmouseover='childrenlit2(\"".getChildrenLit($row2['ID'])."\");' onmouseout='childrenunlit2(\"".getChildrenLit($row2['ID'])."\");'><td  width='300px'><b>".$indent.$row2['name']."</b></td>";
	$query3="select id from users WHERE statistics = 1 ORDER BY id";
	$result3=mysql_query($query3);
	
	while ($row3=mysql_fetch_array($result3)) {
		echo "<td class='stats' onClick='showdetails(\"".$row2['ID']."\",\"".$row3['id']."\");'>".$datetime->tohm($statist->job_on_user($row2['ID'],$row3['id']))."</td>";
		}
	echo "<td class='stats'><b>".$datetime->tohm($statist->return_real_time_allusers($row2['ID']))."</b></td><td class='stats'><b>".$datetime->tohm($statist->job_on_users($row2['ID']))."</b></td></tr>";
	}
	}

echo "<tr ><th></th>";
$query3="select id from users WHERE statistics = 1 ORDER BY id";
$result3=mysql_query($query3);
while ($row3=mysql_fetch_array($result3)) {
	echo "<th class='statsheader'>".$datetime->tohm($statist->user_on_jobs($row3['id']))."</th>";
	}
echo "<th></th></tr></table></div>";



}



if ($_POST['style'] == "real") {
echo "<b>".$loc->statisticsfor.$_POST['datefrom']." - ".$_POST['dateto']."</b><br>";
echo "<div class='maindiv'id='attendanceshow'><table id='statstable'>";


//first header row
$query="select name from users where statistics=1 ORDER BY id";
$result=mysql_query($query);
echo "<tr ><th> </th>";
while ($row=mysql_fetch_array($result)) {
echo "<th class='statsheader'>".$row['name']."</th>";
}
echo "<th colspan=2 class='statsheader'> TOTAL TIME </th></tr>";


//second header row
$query="select name from users where statistics=1 ORDER BY id";
$result=mysql_query($query);
echo "<tr ><th> </th>";
while ($row=mysql_fetch_array($result)) {
echo "<th class='statsheader'>time</th>";
}
echo "<th class='statsheader'> real</th><th class='statsheader'> accumulated </th> </tr>";


$query2="select ID,name,LEVEL from tree WHERE statistics = 1 AND ID != 1 ORDER BY left_state ";
$result2=mysql_query($query2);
while ($row2=mysql_fetch_array($result2)) {
	$indent=indent($row2['LEVEL']);
	if ($statist->job_on_users($row2['ID']) !=0 ) {
	echo "<tr class='statsok' id='row_".$row2['ID']."' onmouseover='childrenlit2(\"".getChildrenLit($row2['ID'])."\");' onmouseout='childrenunlit2(\"".getChildrenLit($row2['ID'])."\");'><td  width='400px'><b>".$indent.$row2['name']."</b></td>";
	$query3="select id from users WHERE statistics = 1 ORDER BY id";
	$result3=mysql_query($query3);
	
	while ($row3=mysql_fetch_array($result3)) {
		echo "<td class='stats' onClick='showdetails(\"".$row2['ID']."\",\"".$row3['id']."\");'>".$datetime->tohm($statist->return_real_time($row2['ID'],$row3['id']))."</td>";
		}
	echo "<td class='stats'><b>".$datetime->tohm($statist->return_real_time_allusers($row2['ID']))."</b></td><td class='stats'><b>".$datetime->tohm($statist->job_on_users($row2['ID']))."</b></td></tr>";
	}
	}

echo "<tr ><th></th>";
$query3="select id from users WHERE statistics = 1 ORDER BY id";
$result3=mysql_query($query3);
while ($row3=mysql_fetch_array($result3)) {
	echo "<th class='statsheader' >".$datetime->tohm($statist->user_on_jobs($row3['id']))."</th>";
	}
echo "<th></th></tr></table></div>";


}






}
}
?>
